import { LinValidator, Rule } from "../code/lin-validator-v2"
import MaterialType from "../sql/module/factory/material_type"

export class TypeValidator extends LinValidator {
    constructor() {
        super()
        this.name = [
            new Rule('isLength', '材料类型名字数小于20', { max: 20 })
        ]
    }
}

export class MateriaValidator extends LinValidator {
    constructor() {
        super()
        this.name = [
            new Rule('isLength', '材料名字数小于30', { max: 30 })
        ]
    }
    async validateTypeId(vals: { body: { MaterialTypeId: any } }) {
        const id = vals.body.MaterialTypeId
        if (!id) throw new Error('材料类型不存在')
        const material = await MaterialType.findOne({
            where: { id }
        })
        if (!material) throw new Error('材料类型不存在')
    }
}
export class UpdateMateriaValidator extends LinValidator {
    constructor() {
        super()
        this.id = []
        this.name = [
            new Rule('isOptional'),
            new Rule('isLength', '材料名字数小于30', { max: 30 })
        ]
        this.phone = [
            new Rule('isOptional'),
            new Rule('isMobilePhone', '请输入正确手机号')
        ]
    }
}